<?php 

class LsFormModel
extends CFormModel
{
	
	protected $arName = '';
	protected $pkAttr = 'id';
	protected $treeSectionAttr = '';
	
	public $ar;
	public $treeParent;
	
	public function attributeNames(){
		$names = parent::attributeNames();
		foreach($names as $i => $name){
			if(in_array($name, array('ar', 'treeParent'))){
				unset($names[$i]);
			}
		}
		$names = array_values($names);
		return $names;
	}
	
	protected function arModel($new = false){
		return $new
			? new $this->arName
			: LsActiveRecord::model($this->arName)
		;
	}
	
	public function loadAr(){
		if($this->ar){
			return $this->ar;
		}
		$this->ar = $this->arModel()->findByPk($this->{$this->pkAttr});
		return $this->ar;
	}
	
	public function validate_pk($attribute,$params){
		$value = $this->$attribute;
		if($value){
			if(!$this->loadAr()){
				$this->addError($this->pkAttr, Yii::t('p_yii', 'Object not found.'));
			}
		}else{
			$this->ar = $this->arModel(true);
		}
	}
 	
	public function validate_treeParent($attribute,$params){
		$value = &$this->$attribute;
		if($value=(int)$value){
			$this->treeParent = $this->arModel()->findByAttributes(array(
				$this->pkAttr => $value,
				$this->treeSectionAttr => $this->{$this->treeSectionAttr},
			));
			if(!$this->treeParent){
				$this->addError($attribute, Yii::t('p_yii', 'Parent object not found.'));
			}
		}else{
			$value = $this->treeParent = null;
		}
	}
	
	public function validate_user($attribute,$params){
		$value = &$this->$attribute;
		if($value){
			if($value != $this->loadAr()->$attribute){
				$this->addError($attribute,Yii::t('p_yii','Unable to change user ID.'));
			}
		}else{
			$value = Yii::app()->user->id;
			if(!$value){
				$this->addError($attribute,Yii::t('p_yii','Unable to get user ID.'));
			}
		}
	}
	
	public function validate_date($attribute,$params){
		$value = &$this->$attribute;
		if($value){
			if($this->loadAr()->$attribute > $value){
				$this->addError($attribute,Yii::t('p_yii','Changing the date in the past is impossible.'));
			}
		}else{
			$value = time();
		}
	}
	
	public function validate_valueOrNull($attribute,$params){
		$value = &$this->$attribute;
		if($value){
			if(isset($params['type'])){
				settype($value, $params['type']);
			}
		}else{
			$value = null;
		}
	}
	
	public function attrId($attribute){
		return CHtml::activeId($this,$attribute);
	}
	
	public function getIsTreeAr(){
		$behaviors = $this->arModel()->behaviors();
		return isset($behaviors['tree']);
	}
	
	public function save(){
		if(!$this->loadAr() && !$this->validate()){
			if(YII_DEBUG){
				throw new CException(
					'Form Model '.get_class($this).' validation fail. Errors: '
					.CVarDumper::dumpAsString($this->errors)
				);
			}
			return false;
		}
		$this->ar->attributes = $this->attributes;
		if($this->getIsTreeAr()){
			if($this->treeParent){
				$result = $this->ar->tree->appendTo($this->treeParent);
			}else{
				$result = $this->ar->tree->save();
			}
		}else{
			$result = $this->ar->save();
		}
		if(!$result && YII_DEBUG){
			throw new CException(
				'AR '.get_class($this->ar).' save fail. Errors: '
				.CVarDumper::dumpAsString($this->ar->errors)
			);
		}
		return $result;
	}
	
	
}

?>